/**
 * Patient.java
 * @author Peter
 */
package edu.gatech.coc.betameisters.people;

import java.io.IOException;

import edu.gatech.coc.betameisters.infoholders.Constants.UserType;
import edu.gatech.coc.betameisters.infoholders.DoctorsOrders;
import edu.gatech.coc.betameisters.infoholders.MedicalHistory;
import edu.gatech.coc.betameisters.utilities.Database;
import edu.gatech.coc.betameisters.utilities.Serializer;


/**
 * The Patient class; this represents patients in our system
 * @author Peter
 *
 * @version $Revision: 1.0 $
 */
public class Patient extends Person{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 4936475513544553874L;

	/**
	 * Patient's doctor's orders
	 */
	private DoctorsOrders myDocOrders;
	
	/**
	 * Patient's medical history
	 */
	private MedicalHistory myMedHist;

	/**
	 * Constructor that creates patients
	 * @param fName
	 * @param lName
	 */
	public Patient(final String fName, final String lName) { // $codepro.audit.disable com.instantiations.assist.eclipse.analysis.audit.rule.effectivejava.constructorsOnlyInvokeFinalMethods
		super(fName, lName);
		personType = UserType.PATIENT;
		myMedHist = new MedicalHistory();
		myDocOrders = null;
		MYLOGGER.info("In Patient constructor");
	}
	
	/**
	 * Database patient constructor
	 * @param username Username
	 * @param password password
	 * @param fName First name
	 * @param lName last name
	 * @param medHist medical history
	 * @param docOrders doctors orders
	 */
	public Patient(final String username, final String password,   // $codepro.audit.disable com.instantiations.assist.eclipse.analysis.audit.rule.effectivejava.constructorsOnlyInvokeFinalMethods, largeNumberOfParameters
			final String fName, final String lName, final MedicalHistory medHist,
			final DoctorsOrders docOrders) {
		super(username, password, fName, lName);
		personType = UserType.PATIENT;
		this.myDocOrders = docOrders;
		this.myMedHist = medHist;
		MYLOGGER.info("In Patient constructor");
	}

	/**
	 * Default patient constructor for database use
	 */
	public Patient() {
		super(); // $codepro.audit.disable invocationOfDefaultConstructor
		personType = UserType.PATIENT;
		myMedHist = new MedicalHistory();
		myDocOrders = new DoctorsOrders("", "", "");
		MYLOGGER.info("In Patient constructor");
	}

	/**
	 * Ideally a button on the UI, 
	 * when pressed this method would call on display to display information
	 * about patient details. 
	 * 
	 * @return Patient's Medical History
	 */
	public MedicalHistory viewMedHist(){
		MYLOGGER.info("Patient is trying to view Medical History");
		return myMedHist;
	}
	
	/**
	 * getter for medical history
	 * @return the patients medical history
	 */
	public MedicalHistory getMedHist(){
		return myMedHist;
	}
	
	/**
	 * Updates the patient's medical history
	 * @param recentMedHist - Patient's Updated Medical History
	 */
	public void updateMedHist(MedicalHistory recentMedHist){
		// update to the database
		String serialized="";
		try {
			serialized = Serializer.toString(recentMedHist);
		} catch (IOException e) {
			e.printStackTrace();
		}
		final String medHistUpdate = "UPDATE `patient` " +
				"SET `medical_history`='" + serialized + "' " +
				"WHERE `username`= '" + this.getUserID() + "'";
		Database.executeUpdateQuery(medHistUpdate); // $codepro.audit.disable com.instantiations.assist.eclipse.analysis.unusedReturnValue
		myMedHist = recentMedHist;
	}
	
	/** // $codepro.audit.disable unnecessaryOverride
	 * Calls default create for database use
	 */
	public void create() { // $codepro.audit.disable unnecessaryOverride
		super.create();
	}

	/**
	 * Sets the patient's doctor's orders and updates the database
	 * @param myDocOrders doctors orders object
	 * @param docUserID user id for the doctor
	 */
	public void setMyDocOrders(DoctorsOrders myDocOrders, String docUserID) {
		// update to the database
		String serialized="";
		try {
			serialized = Serializer.toString(myDocOrders);
		} catch (IOException e) {
			e.printStackTrace();
		}
		String medHistUpdate="";
		// if null insert the row; else update the row
		if(this.myDocOrders == null) {
			medHistUpdate = "INSERT INTO `doctors_orders` " +
					"(`patient_username`, `doctor_username`, `orders`) " +
					"VALUES ('" + this.getUserID() + "', '" + 
					docUserID + "', '" + serialized + "')";
		}
		else {
			medHistUpdate = "UPDATE `doctors_orders` " +
			"SET `orders`='" + serialized + "', `doctor_username`= '" + docUserID + "' " +
			"WHERE `patient_username`= '" + this.getUserID() + "'";
		}
		Database.executeUpdateQuery(medHistUpdate); // $codepro.audit.disable com.instantiations.assist.eclipse.analysis.unusedReturnValue
		this.myDocOrders = myDocOrders;
	}

	/**
	 * Gets the patient's doctor's orders
	 * @return doctor's order
	 */
	public DoctorsOrders getMyDocOrders() {
		return myDocOrders;
	}
	
	/**
	 * Overrides the default toString method
	 *
	 * @return Patient's name 
	 */
	public String toString(){
		return "Patient: " + this.getFirstName() + " " + this.getLastName();
	}
}
